import datetime

from . import db

class StuInfo(db.Model):
    __tablename__ = "stuinfo"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    stu_name = db.Column(db.String(128),nullable=False)
    stu_age = db.Column(db.Integer, nullable=False)
    stu_address = db.Column(db.String(256), nullable=False)
    class_id = db.Column(db.ForeignKey('class_info.id'))
    # stu_class = db.Column(db.String(256), nullable=False)
    add_time = db.Column(db.DateTime, default = datetime.datetime.now)
    extra_attr = "abc"

    @classmethod
    def create_stu(cls,stuname,stuage,stuaddress,stuclass):
        stu = cls()
        stu.stu_name = stuname
        stu.stu_age = stuage
        stu.stu_address = stuaddress
        stu.stu_class = stuclass
        db.session.add(stu)
        db.session.commit()

    @classmethod
    def del_stu(self,id):
        stu = StuInfo.query.get(id)
        if stu:
            db.session.delete(stu)
            db.session.commit()

    @classmethod
    def mod_stu(cls,id,stuname,stuage,stuaddress,stuclass):
        stu = StuInfo.query.get(id)
        stu.stu_name = stuname
        stu.stu_age = stuage
        stu.stu_address = stuaddress
        stu.stu_class = stuclass

        db.session.add(stu)
        db.session.commit()

    def keys(self):
        return ("id","stu_name","stu_age","stu_address","stu_class")

    def __getitem__(self, key):
        if key == "stu_class":
            return self.stu_class.class_name
        return getattr(self,key)

    @classmethod
    def get_student(cls,id):
        stu = cls.query.get(id)
        tmp_dict = dict(stu)
        return tmp_dict

    @classmethod
    def get_students(cls):
        stus = StuInfo.query.all()
        stu_lst = [dict(stu) for stu in stus]
        return stu_lst

class ClassInfo(db.Model):
    __tablename__ = "class_info"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    class_name = db.Column(db.String(256), nullable=False)
    class_teacher = db.Column(db.String(256), nullable=False)
    add_time = db.Column(db.DateTime, default=datetime.datetime.now)
    stus = db.relationship("StuInfo",backref="stu_class")

    def keys(self):
        return ("id", "stu_name", "stu_age", "stu_address", "stu_class")

    def __getitem__(self, key):
        return getattr(self, key)

stu_course = db.Table("stu_course",
                      db.Column("stu_id", db.ForeignKey("stuinfo.id")),
                      db.Column("course_id", db.ForeignKey("course_info.id")),
                      db.Column("add_time", db.DateTime, default=datetime.datetime.now))

class CourseInfo(db.Model):
    __tablename_ = "course_info"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    course_name = db.Column(db.String(256), nullable=False)
    course_teacher = db.Column(db.String(256), nullable=False)
    add_time = db.Column(db.DateTime, default=datetime.datetime.now)

    stus = db.relationship("StuInfo", secondary=stu_course, backref="course")

# tmp_dict = {}
# tmp_dict["stu_id"] = stu.id
# tmp_dict["stu_name"] = stu.stu_name
# tmp_dict["stu_age"] = stu.stu_age
# tmp_dict["stu_address"] = stu.stu_address
# tmp_dict["stu_class"] = stu.stu_class